package com.loong.news.handler;

import com.loong.common.domain.BaseQuery;
import com.loong.common.domain.Pager;
import com.loong.common.utils.TransformUtils;
import com.loong.news.business.NewsOperationRecordBusiness;
import com.loong.news.common.backend.result.NewsOperationRecordResult;
import com.loong.news.mapper.NewsOperationRecordMapper;
import com.loong.news.model.NewsOperationRecord;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.Date;
import java.util.List;

/**
 * @Description:
 * @Author: 刘群
 * @Date: 2020/10/15 17:49
 */
@Service
public class NewsOperationRecordHandler {

    @Resource
    private NewsOperationRecordBusiness recordBusiness;

    @Resource
    private NewsOperationRecordMapper recordMapper;


    /**
     * 新增新闻操作记录到数据库：
     * @param account
     * @param operationType
     * @param time
     * @param newsId
     */
    public void saveNewsOperationRecord(String account, String operationType, Date time, Long newsId){
        NewsOperationRecord record = NewsOperationRecord.builder()
                .account(account)
                .operationType(operationType)
                .operationTime(time)
                .newsId(newsId).build();
        record.setCreateTime(time);
        recordBusiness.save(record);
    }

    /**
     * 分页查询新闻的操作记录
     * @param newsId 新闻id
     * @return 分页结果
     */
    public Pager<NewsOperationRecordResult> findRecordsByNewsId(BaseQuery query,Long newsId){
        Pager<NewsOperationRecord> pager = recordBusiness.pageQuery(query, () -> recordMapper.findRecordsByNewsId(newsId));
        return TransformUtils.transformPage(pager,NewsOperationRecordResult.class);
    }

    /**
     * 根据新闻id删除该新闻所有的操作记录
     * @param idList
     */
    public void deleteRecordsByNewsId(List<Long> idList){
        for (Long id : idList) {
            recordMapper.deleteRecordsByNewsId(id);
        }
    }


}
